/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simchewie;

import java.io.*;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author willi
 */
public class Labyrinth {

    private Field[][] fields = new Field[10+2][12+2];
    private boolean isFinished;
    public int finx = -1;
    public int finy = -1;

    public void save(File f) {
        BufferedWriter out = null;
        String c = "";
        try {
            out = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(f)));
            for (int i = 0; i < 10+2; i++) {
                for (int j = 0; j < 12+2; j++) {
                    c = "";
                    if (fields[i][j].north) {
                        c += "1";
                    } else {
                        c += "0";
                    }
                    if (fields[i][j].east) {
                        c += "1";
                    } else {
                        c += "0";
                    }
                    if (fields[i][j].south) {
                        c += "1";
                    } else {
                        c += "0";
                    }
                    if (fields[i][j].west) {
                        c += "1";
                    } else {
                        c += "0";
                    }
                    out.write(c, 0, 4);

                }   
            }
            
            out.write(finx +":"+ finy);
            out.newLine();
        } catch (IOException ex) {
            System.out.println(ex);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(Labyrinth.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void load(File f) {
        char[] c = new char[4];
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader(
                    new FileInputStream(f)));


            for (int i = 0; i < 10+2; i++) {
                for (int j = 0; j < 12+2; j++) {
                    c = new char[4];
                    in.read(c, 0, 4);

                    if (c[0] == '1') {
                        fields[i][j].north = true;
                    } else {
                        fields[i][j].north = false;
                    }
                    if (c[1] == '1') {
                        fields[i][j].east = true;
                    } else {
                        fields[i][j].east = false;
                    }
                    if (c[2] == '1') {
                        fields[i][j].south = true;
                    } else {
                        fields[i][j].south = false;
                    }
                    if (c[3] == '1') {
                        fields[i][j].west = true;
                    } else {
                        fields[i][j].west = false;
                    }
                   

                }
            }
            String s = in.readLine();
            
            
            if(Integer.parseInt(s.split(":")[0]) > -1) {
                setFinish(Integer.parseInt(s.split(":")[0]), Integer.parseInt(s.split(":")[1]));
            }

        } catch (FileNotFoundException ex) {
        } catch (Exception ex) {
            System.out.println(ex);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Labyrinth.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public Labyrinth() {
        for (int i = 0; i < 12+2; i++) {
            for (int j = 0; j < 10+2; j++) {
                fields[j][i] = new Field();
            }
        }
    }

    public Field getField(int x, int y) {

        return fields[x][y];
    }

    public void setFinish(int x, int y) {
        isFinished = true;
        finx = x;
        finy = y;
    }

    public boolean isFinished() {
        return isFinished;
    }

    public boolean isFinish(int x, int y) {
        if (isFinished) {
            return finx == x && finy == y;
        } else {
            return false;
        }
    }

    public int countField(int x, int y) {
        int c = 0;
        if (fields[x][y].north) {
            c++;
        }
        if (fields[x][y].south) {
            c++;
        }
        if (fields[x][y].west) {
            c++;
        }
        if (fields[x][y].east) {
            c++;
        }

        return c;

    }



    public boolean checkOrientation(int x, int y, int o) {


        switch (o) {
            case 0:
                return fields[x][y].north;
            case 1:
                return fields[x][y].east;
            case 2:
                return fields[x][y].south;
            case 3:
                return fields[x][y].west;
            default:
                return false;
        }
    }

    public void setField(int x, int y, String s, boolean b) {
        
        if (s.equals("north")) {
            fields[x][y].north = b;
            if (y - 1 >= 0) {
                fields[x][y - 1].south = b;
                
            }
        } else if (s.equals("east")) {

            fields[x][y].east = b;
            if (x + 1 <= 9+2) {
                fields[x + 1][y].west = b;
                
            }
        } else if (s.equals("south")) {
            fields[x][y].south = b;
            if (y + 1 <= 11+2) {
                fields[x][y + 1].north = b;
               
            }
        } else if (s.equals("west")) {
            fields[x][y].west = b;
            if (x - 1 >= 0) {
                fields[x - 1][y].east = b;
                
            }
        }
    }


}
